package DataBase;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class FrequencyUnitManager {
	public static DbcpBean dbcp ;
	static{
		dbcp = new DbcpBean();
	}
	public static int insert(FrequencyUnit user) {
//		DbcpBean dbcp = new DbcpBean();
		Connection conn = dbcp.getConnection();
		int result = 0;
		if (conn != null) {
			try {
				Statement stmt = conn.createStatement();
				String sql = "insert into tb_FrequencyUnit (fu_name,fu_nrofdays) values ('"
						+ user.getFu_name()
						+ "' , '"
						+ user.getFu_nrofdays()
						+ "');";
				result = stmt.executeUpdate(sql);
			} catch (SQLException e) {
				e.printStackTrace();
			}			
		}
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return result;
	}
	
	public static ArrayList<FrequencyUnit> select(String fu_name) {
//		DbcpBean dbcp = new DbcpBean();
		Connection conn = dbcp.getConnection();
		ResultSet result = null;
		ArrayList<FrequencyUnit> users = new ArrayList<FrequencyUnit>();
		if (conn != null) {
			try {
				Statement stmt = conn.createStatement();
				String sql = null;
				if (fu_name == "" || fu_name == "*") {
					sql = "select * from tb_FrequencyUnit";
				} else {
					sql = "select * from tb_FrequencyUnit where fu_name = '"
							+ fu_name + "';";
				}
				result = stmt.executeQuery(sql);
				while (result.next()) {
					FrequencyUnit user = new FrequencyUnit();
					user.setFu_ID(result.getInt("fu_ID"));
					user.setFu_name(result.getString("fu_name"));
					users.add(user);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		return users;
	}
	
	
	
	public static ArrayList<FrequencyUnit> select(int fu_ID) {
//		DbcpBean dbcp = new DbcpBean();
		Connection conn = dbcp.getConnection();
		ResultSet result = null;
		ArrayList<FrequencyUnit> users = new ArrayList<FrequencyUnit>();
		if (conn != null) {
			try {
				Statement stmt = conn.createStatement();
				String sql = null;
				String i = Integer.toString(fu_ID);
				if (i == "" || i == "*") {
					sql = "select * from tb_FrequencyUnit";
				} else {
					sql = "select * from tb_FrequencyUnit where fu_ID = '"
							+ fu_ID + "';";
				}
				result = stmt.executeQuery(sql);
				while (result.next()) {
					FrequencyUnit user = new FrequencyUnit();
					user.setFu_ID(result.getInt("fu_ID"));
					user.setFu_name(result.getString("fu_name"));
					user.setFu_nrofdays(result.getInt("fu_nrofdays"));
					users.add(user);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return users;
	}
    
	
	public static void main(String[] args) {

		/*FrequencyUnit user = new FrequencyUnit();
		user.setFu_name("qw");
		user.setFu_nrofdays(10);
	
		if (FrequencyUnitManager.insert(user) == 0) {
			System.out.println("����");
		}*/
//		System.out.println(FrequencyUnitManager.select("xmx"));
		int i = 9;
		int j=0;
		 try{
		System.out.println(i/j);
		System.out.println("dddddddd");
		 }catch(Exception e){
			 e.printStackTrace();
		 }
		 
		
	}
}


